package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.web.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * 登录
 */
@Controller
public class LoginController {
    @Reference
    private IUserInfoService userInfoService;

    @RequestMapping("/get")
    @ResponseBody
    public Object get(Long id) {
        return userInfoService.get(id);
    }

    @RequestMapping("/checkPhone")
    @ResponseBody
    public Object checkPhone(String phone) {
        //判断,该手机号是否已被注册
        return !userInfoService.checkPhone(phone);
    }

    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone) {
        //发送验证码
        userInfoService.sendVerifyCode(phone);
        return new AjaxResult();
    }

    @RequestMapping("/userRegist")
    @ResponseBody
    public Object userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        //校验
        userInfoService.userRegist(phone, nickname, password, rpassword, verifyCode);
        return new AjaxResult();
    }


    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(String username, String password, HttpServletResponse response) {
        //用户登录信息的校验
        //1.获取账号密码 查询数据库 得到user对象
        //2.判断user对象是否存在 如果存在创建出token
        String token = userInfoService.userLogin(username, password);
        CookieUtil.addCookie("token", token, RedisKeys.TOKEN_USER_INFO.getTime().intValue(), response);
        return new AjaxResult();
    }
}

